1
2
3
4
5
6
7
8
9 package ca.uhn.cache.impl;
10
11 import java.text.ParseException;
12
13 import junit.framework.TestCase;
14
15 import org.springframework.context.support.ClassPathXmlApplicationContext;
16
17 import EDU.oswego.cs.dl.util.concurrent.DirectExecutor;
18 import EDU.oswego.cs.dl.util.concurrent.Executor;
19 import ca.uhn.cache.IDataItem;
20 import ca.uhn.cache.IDimension;
21 import ca.uhn.cache.IQuery;
22 import ca.uhn.cache.IQueryResult;
23 import ca.uhn.cache.exception.DataSourceException;
24 import ca.uhn.cache.helper.InvocationTimer;
25 import ca.uhn.cache.util.SpringTestCaseUtils;
26
27
28 /***
29 * TODO complete javadoc for
30 *
31 * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
32 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:46 $ by $Author: bryan_tripp $
33 */
34 public class SelfCachingDataSourceTest extends TestCase {
35
36
37 static {
38 ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
39 }
40
41 private static final String SELF_CACHING_DATA_SOURCE_BEAN = "mySelfCachingDataSource";
42
43 private static final Executor ourExecutor = new DirectExecutor();
44
45 private SelfCachingDataSource mySelfCachingDataSource;
46 private IDimension myPatientIdDim;
47 private IDimension myActTypesDim;
48 private IDimension myDateRangeDim;
49
50 static interface QueryResultVisitor {
51 public void visit( IQueryResult theQueryResult );
52 public void visit( IDataItem theDataItem );
53 }
54
55
56
57
58 protected void setUp() throws Exception {
59 super.setUp();
60
61 ClassPathXmlApplicationContext appContext =
62 new ClassPathXmlApplicationContext(
63 SpringTestCaseUtils.packageToPath( SelfCachingDataSourceTest.class.getPackage() ) +
64 "/spring-self-caching-data-source.xml");
65
66
67 mySelfCachingDataSource = (SelfCachingDataSource) appContext.getBean( SELF_CACHING_DATA_SOURCE_BEAN );
68 myPatientIdDim = (IDimension) appContext.getBean( "myPatientIdDim" );
69 myActTypesDim = (IDimension) appContext.getBean( "myActTypesDim" );
70 myDateRangeDim = (IDimension) appContext.getBean( "myDateRangeDim" );
71 }
72
73
74
75
76 protected void tearDown() throws Exception {
77 super.tearDown();
78 }
79
80 /***
81 * Make sure we get the same result with two identical consecutive queries.
82 *
83 * @throws ParseException ...
84 * @throws DataSourceException ...
85 */
86 public void test1() throws ParseException, DataSourceException {
87
88 final Query query1 = new Query();
89 query1.addParameter( new StringParam( myPatientIdDim, "patientId1") );
90 query1.addParameter( new StringSetParam( myActTypesDim, new String[] { "DI" } ) );
91 query1.addParameter(
92 DateRangeParam.getInstance( myDateRangeDim, "Jan 1, 2003 00:01 am", "Jan 1, 2006 00:00 am" ) );
93
94 IQueryResult expectedQueryResult = mySelfCachingDataSource.execute( new IQuery[] { query1 } );
95
96 IQueryResult actualQueryResult = mySelfCachingDataSource.execute( new IQuery[] { query1 } );
97
98 assertEquals( expectedQueryResult, actualQueryResult );
99
100 }
101
102 /***
103 * Make sure we get the same result with two identical consecutive queries.
104 *
105 * @throws ParseException ...
106 * @throws DataSourceException ...
107 */
108 public void test2() throws ParseException, DataSourceException {
109
110 final Query query1 = new Query();
111 query1.addParameter( new StringParam( myPatientIdDim, "patientId1") );
112 query1.addParameter( new StringSetParam( myActTypesDim, new String[] { "DI" } ) );
113 query1.addParameter(
114 DateRangeParam.getInstance( myDateRangeDim, "Jan 1, 2003 00:01 am", "Jan 1, 2006 00:00 am" ) );
115
116 final Query query2 = new Query();
117 query2.addParameter( new StringParam( myPatientIdDim, "patientId2") );
118 query2.addParameter( new StringSetParam( myActTypesDim, new String[] { "DI" } ) );
119 query2.addParameter(
120 DateRangeParam.getInstance( myDateRangeDim, "Jan 1, 2003 00:01 am", "Jan 1, 2006 00:00 am" ) );
121
122 IQueryResult expectedQueryResult = mySelfCachingDataSource.execute( new IQuery[] { query1, query2 } );
123
124 IQueryResult actualQueryResult = mySelfCachingDataSource.execute( new IQuery[] { query1, query2 } );
125
126 assertEquals( expectedQueryResult, actualQueryResult );
127 }
128
129 /***
130 * Make sure there is a speeds up when using cached results.
131 *
132 * @throws Exception ...
133 */
134 public void test3() throws Exception {
135
136 final Query query1 = new Query();
137 query1.addParameter( new StringParam( myPatientIdDim, "patientId1") );
138 query1.addParameter( new StringSetParam( myActTypesDim, new String[] { "DI" } ) );
139 query1.addParameter(
140 DateRangeParam.getInstance( myDateRangeDim, "Jan 1, 2003 00:01 am", "Jan 1, 2006 00:00 am" ) );
141
142 InvocationTimer tiNotUsingCache = new InvocationTimer() {
143 protected Object doInvoke() throws Exception {
144 return mySelfCachingDataSource.execute( new IQuery[] { query1 } );
145 }
146 };
147 tiNotUsingCache.invoke();
148
149
150 InvocationTimer tiUsingCache = new InvocationTimer() {
151 protected Object doInvoke() throws Exception {
152 return mySelfCachingDataSource.execute( new IQuery[] { query1 } );
153 }
154 };
155 tiUsingCache.invoke();
156
157 assertTrue( tiUsingCache.getElapsedTime() < tiNotUsingCache.getElapsedTime() / 10 );
158
159 }
160
161 }